<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 添加商品区域 -->
      <el-row :gutter="20">
        <el-col :span="10">
          <!-- 输入框部分 -->
          <!-- clearable表示可清空 -->
          <!-- clear表示点击重置 -->
          <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear="getGoodList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <!-- 按钮部分 -->
          <el-button type="primary" plain @click="toAddGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表表格 -->
      <el-table :data="goodList" style="width: 100%" border stripe>
        <!-- 索引列 -->
        <el-table-column align="center" type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="680"></el-table-column>
        <el-table-column align="center" prop="goods_price" label="商品价格（元）"></el-table-column>
        <el-table-column align="center" prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column align="center" prop="add_time" label="创建时间"></el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="warning" plain icon="el-icon-edit" size="mini"></el-button>
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="removeGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'goodList',
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1, // 当前页的页码数
        pagesize: 10 // 每页显示的条目数
      },
      goodList: [], // 用来存储用户商品列表数据
      // 总共的条目数
      total: 0
    }
  },
  created() {
    // 调用获取商品列表总数居的方法
    this.getGoodList()
  },
  methods: {
    toAddGoods() {
      this.$router.push('/goods/addgoods')
    },

    // 删除商品
    async removeGoods(id) {
      const resultMsg = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (resultMsg !== 'confirm') {
        return this.$message.info('您取消啦删除')
      }
      const { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getGoodList()
    },
    // 当改变页码时
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodList()
    },
    // 当改变每页的显示数目时
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodList()
    },

    // 获取所有商品列表的数据
    async getGoodList() {
      const { data: res } = await this.$http.get('/goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      res.data.goods.forEach(item => {
        item.add_time = this.$dayjs(item.add_time).format('YYYY-MM-DD hh:mm')
      })
      this.goodList = res.data.goods
      this.total = res.data.total
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 15px;
}
</style>
